package com.qinyi.framework.web.exception;

import com.qinyi.common.ajax.AjaxCode;
import com.qinyi.common.ajax.CommonAjaxResult;
import com.qinyi.common.exception.CustomException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author wangan on 2019/6/5
 * @description
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

	public static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

	@ExceptionHandler(CustomException.class)
	public CommonAjaxResult handleCustomException(Exception e) {
		CustomException exception = (CustomException) e;
		System.out.println(new CommonAjaxResult(exception.getAjaxCode().getCode(), exception.getMsg()).toString());
		return new CommonAjaxResult(exception.getAjaxCode().getCode(), exception.getMessage());
	}

	@ExceptionHandler(Exception.class)
	public CommonAjaxResult Exception(Exception e) {
		log.error("" + e);
		log.info(new CommonAjaxResult(AjaxCode.SERVER_ERROR, "系统异常").toString());
		return new CommonAjaxResult(AjaxCode.SERVER_ERROR, "系统异常");
	}
}
